package main

import (
	"context"
	"fmt"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"

	"go-code/src/grpc_test/proto"
)
type Server struct{
	proto.UnimplementedGreeterServer
}

func (s *Server) SayHello(ctx context.Context,request *proto.HelloRequest) (*proto.HelloReply,
	error){
		md, ok := metadata.FromIncomingContext(ctx)
		if ok {
			fmt.Print("get metadata error",ok)
		}
		for key , val := range md{
			fmt.Println(key,val)
		}
		if nameSlice, ok := md["name"]; ok {
			fmt.Println(nameSlice)
			for i, e := range nameSlice {
				fmt.Println(i, e)
			}
		}
		return &proto.HelloReply{
			Message: "hello" + request.Name,
		},nil
}

// func (s *Server) mustEmbedUnimplementedGreeterServer() {
// 	// 嵌入 UnimplementedGreeterServer 类型，保证接口完整性
// }

func main(){
	g := grpc.NewServer()
	proto.RegisterGreeterServer(g, &Server{})
	lis, err := net.Listen("tcp","0.0.0.0:8087")
	if err != nil{
		panic("fail 通listen："+ err.Error())
		}	
		err = g.Serve(lis)
		if err != nil{
			panic("failed to start grpc:" + err.Error())
		}
}